package com.example.rqweb.Okhttp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.rqweb.R;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkActivity extends AppCompatActivity {
 Button b1,b,b2,b3;
 TextView t1;
 okhttp op;
    Response res;

    //创建客户端连接对象
    OkHttpClient htp=new OkHttpClient.Builder().
            retryOnConnectionFailure(true).build();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.okhtp_main);

        initb();
        //同步请求
        op=new okhttp();
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               StringBuilder bp=new StringBuilder();
              /* bp.append(tb());
               t1.setText(bp.toString());*/
             // t1.setText(tb());
            }
        });
        //异步请求
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                op.yb();
            }
        });
        //同步请求


    }
    public  void initb()
    {


        b=findViewById(R.id.bt1);
        b1=findViewById(R.id.bt2);
       b2=findViewById(R.id.button);
        t1=findViewById(R.id.textView);

    }

    public void tb() throws IOException {

        Request requ=new Request.Builder().get().url("https://www.baidu.com").build();

        try {
            res = htp.newCall(requ).execute();

        }
        catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(res.body().string());
       // return  res.body().toString();

    }

    public void yb() {

        Request requ = new Request.Builder().get().url("https://www.baidu.com").build();

        {

            htp.newCall(requ).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e)
                {
                    System.out.println("调用失败");
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    System.out.println("调用成功"+res.body().toString());

                }
            });


        }
    }



}
